<?php
include ('chek.php');
//修改管理员账号或密码
$aid=$input->get('aid');
$auser=array(
    'auser'=>'',
    'apass'=>'',
);
if($aid>0){
    $sql="select * from admin WHERE aid='{$aid}'";
    $res=$db->query($sql);
    $auser=$res->fetch_array(MYSQLI_ASSOC);
}

//添加管理员
if($input->get('do')=='add'){
    $auser=$input->post('auser');
    $apass=$input->post('apass');
    if(empty($auser) or empty($apass)){
        die('账号或密码不能为空');
    }
    //用户名是否重复
    $sql1="select * from admin WHERE auser='{$auser}' and aid <> '{$aid}'";
    $mysql_result1=$db->query($sql1);
    if($mysql_result1->fetch_array()){
        die('账号不能重复');
    }
    //插入管理员
    if($aid<1){
        $sql="insert into admin (auser,apass) VALUES ('$auser','$apass')";
    }
    else{
        $sql="UPDATE admin SET auser='{$auser}',apass='{$apass}' WHERE aid='{$aid}'";

    }
    $mysql_result=$db->query($sql);
    if($mysql_result){
        header('location:auser.php');
    }else{
        die('执行失败');
    }
}
?>
<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8"/>
    <title>添加管理员</title>
    <?php include (PATH.'/header.inc.php'); ?>

</head>
<body>
<?php include ('./nav.inc.php')?>
<div class="container">
    <div class="row">
        <h1>管理员添加<small class="pull-right"><a class="btn btn-default" href="auser.php">返回</a></small></h1>
        <hr/>
        <form class="form-horizontal" action="auser_add.php?do=add&aid=<?php echo $aid?>" method="post">
            <div class="form-group">
                <label for="inputEmail3" class="col-sm-2 control-label">管理员</label>
                <div class="col-sm-6">
                    <input type="text" class="form-control" name="auser" placeholder="请输入用户名" value="<?php echo $auser['auser']?>">
                </div>
            </div>
            <div class="form-group">
                <label for="inputPassword3" class="col-sm-2 control-label">密码</label>
                <div class="col-sm-6">
                    <input type="password" class="form-control" name="apass" placeholder="请输入密码" value="<?php echo $auser['apass']?>">
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-6">
                    <button type="submit" class="btn btn-default"> 提交</button>
                </div>
            </div>
        </form>
    </div>
</div>
</body>
</html>